﻿var ajaxUrl = '/hx.aspx';
function Login() {
    var custName = $("#Name").val();
    var custPwd = $("#Pwd").val();
    if ($.trim(custName) == "") {
        alert("请输入用户名");
        return;
    }
    else if ($.trim(custPwd) == "") {
        alert("请输入密码");
        return;
    }
    else {
        custLogin(custName, custPwd);
    }
}

function changeCode() {
    var img = $('#imgCode');
    var ran = Math.random() * 1000;
    img.attr('src', '/VerifyImagePage.aspx?randomcode=' + ran);
}

function textChange(txtCtrl, msgCtrlName, length, msg) {
    $(msgCtrlName).css('display', 'none');
    var value = $.trim(txtCtrl.value);
    if (value.length < length) {
        showMessage(msgCtrlName, msg);
    }
}
function custLogin(custName, custPwd) {
    $.getJSON(ajaxUrl, { oper: "custLogin", custName: custName, custPwd: custPwd }, function(data) {
        if (data && data.msg == "suc") {
            location.href = "/ProductList.aspx";
        }
        else {
            alert("用户名或密码错误");
            $("#Name").val("");
            $("#Pwd").val("");
        }
    });
}

function checkName() {
    var name = $("#userid").val();
    var user = "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$";
    if ($.trim(name) == "" || name == "中、英文均可，12字符以内") {
        $("#username_error").css("color", "red");
        $("#username_error").text("请输入用户名");
        return false;
    }
    else if (!RegExp(user).test(name)) {
        $("#username_error").css("color", "red");
        $("#username_error").text("由4-12位中文、英文、数字及“_”、“-”组成");
        return false;
    }
    else if (name.length < 4) {
        $("#username_error").css("color", "red");
        $("#username_error").text("用户名长度只能在4-12位字符之间");
        return false;
    }
    else if (name.length > 12) {
        $("#username_error").css("color", "red");
        $("#username_error").text("用户名长度只能在4-12位字符之间");
        return false;
    }
    else {
        $.getJSON(ajaxUrl, { oper: "checkUserName", userName: name }, function(data) {
            if (data && data.msg == "exists") {
                $("#username_error").css("color", "red");
                $("#username_error").text("此用户已经存在！");
                return false;
            }
            else {
                return true;
            }
        });
    }
}
function enter() {
    var value = $("#userid").val();
    if (value == "中、英文均可，12字符以内") {
        $("#userid").val("");
        $("#userid").css("color", "black");
    }
    $("#username_error").html("");
}
function enter1() {
    $("#pwd_error").html("");
}
function enter2() {
    $("#pwd2_error").html("");
}
function enter3() {
    $("#mail_error").html("");
    var value = $("#email").val();
    if (value == "请输入正确邮箱便于找回密码") {
        $("#email").val("");
        $("#email").css("color", "black");
    }
}
function enter4() {
    $("#otherName_error").html("");
}
function enter5() {
    $("#tel_error").html("");
}
function enter6() {
    var value = $("#other").val();
    if (value == "如:QQ 12345678") {
        $("#other").val("");
    }
}
function enter7() {
    $("#otherName_error").html("");
}
function checkPwd() {
    var pwd = /^[A-Za-z0-9_-]+$/;
    var password = $("#userpwd").val();
    if ($.trim(password) == "") {
        $("#pwd_error").css("color", "red");
        $("#pwd_error").text("请输入密码");
        return false;
    }
    else if (!RegExp(pwd).test(password)) {
        $("#pwd_error").css("color", "red");
        $("#pwd_error").text("密码由英文、数字及“_”、“-”组成");
        return false;
    }
    else if (password.length < 6 || password.length > 20) {
        $("#pwd_error").css("color", "red");
        $("#pwd_error").text("密码长度只能在6-20位字符之间");
        return false;
    }
    else {
        return true;
    }
}
function checkPwdOk() {
    var pwd1 = $("#userpwd").val();
    var pwd2 = $("#userpwdok").val();
    if ($.trim(pwd2) == "") {
        $("#pwd2_error").css("color", "red");
        $("#pwd2_error").text("请输入密码");
        return false;
    }
    else if ($.trim(pwd1) != $.trim(pwd2)) {
        $("#pwd2_error").css("color", "red");
        $("#pwd2_error").text("两次输入的密码不一致");
        return false;
    }
    else {
        return true;
    }
}
function checkEmail() {
    var emailVal = $("#email").val();
    var email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
    if ($.trim(email) == "") {
        $("#mail_error").css("color", "red");
        $("#mail_error").text("请输入邮箱");
        return false;
    }
    else if (!RegExp(email).test(emailVal)) {
        $("#mail_error").css("color", "red");
        $("#mail_error").text("邮箱格式不正确");
        return false;
    }
    else {
        $.getJSON(ajaxUrl, { oper: "checkUserEmail", userEmail: emailVal }, function(data) {
            if (data && data.msg == "exists") {
                $("#mail_error").css("color", "red");
                $("#mail_error").text("此邮箱已经存在！");
                return false;
            }
            else if (data && data.msg == "suc") {
                return true;
            }
        });
    }
}
function checkOtherName() {
    var otherName = $("#name").val();
    if ($.trim(otherName) == "") {
        $("#otherName_error").css("color", "red");
        $("#otherName_error").text("请输入联系人姓名");
        return false;
    }
    else {
        return true;
    }
}
function checkTel() {
    var tel = /^[0-9_-]+$/;
    var telphone = $("#tel").val();
    if ($.trim(telphone) == "") {
        $("#tel_error").css("color", "red");
        $("#tel_error").text("请输入联系人电话");
        return false;
    }
    else if (!RegExp(tel).test(telphone)) {
        $("#tel_error").css("color", "red");
        $("#tel_error").text("只能是纯数字");
        return false;
    }
    else {
        return true;
    }
}
function checkWay() {
    var val = $('input:radio[name="way"]:checked').val();
    if (val == null) {
        alert("请选择了解途径");
        return false;
    }
    else {
        return true;
    }
}
function checkCode() {
    var code = $("#txtIdentityCode").val();
    if ($.trim(code) == "") {
        $("#codeError").val("请输入验证码");
    }
    else {
        $.getJSON(ajaxUrl, { oper: "checkCode", code: code }, function(data) {
            if (data && data.msg == "err") {
                $("#codeError").css("color", "red");
                $("#codeError").text("验证码错误");
                return false;
            }
            else {
                $("#codeError").css("color", "black");
                $("#codeError").text("");
                return true;
            }
        });
    }
}
function checkSubmit() {
    var name = $("#userid").val();

    var user = "^[A-Za-z0-9_\\-\\u4e00-\\u9fa5]+$";
    if ($.trim(name) == "" || name == "中、英文均可，12字符以内") {
        $("#username_error").css("color", "red");
        $("#username_error").text("请输入用户名");
        return;
    }
    else if (!RegExp(user).test(name)) {
        $("#username_error").css("color", "red");
        $("#username_error").text("由4-12位中文、英文、数字及“_”、“-”组成");
        return;
    }
    else if (name.length < 4) {
        $("#username_error").css("color", "red");
        $("#username_error").text("用户名长度只能在4-12位字符之间");
        return;
    }
    else if (name.length > 12) {
        $("#username_error").css("color", "red");
        $("#username_error").text("用户名长度只能在4-12位字符之间");
        return;
    }
    else {
        $.getJSON(ajaxUrl, { oper: "checkUserName", userName: name }, function(data) {
            if (data && data.msg == "exists") {
                $("#username_error").css("color", "red");
                $("#username_error").text("此用户已经存在！");
                return;
            }
        });
    }
    var pwd = checkPwd();
    var pwd2 = checkPwdOk();
    if (pwd == true && pwd2 == true) {
        var emailVal = $("#email").val();
        var email = "^\\w+((-\\w+)|(\\.\\w+))*\\@[A-Za-z0-9]+((\\.|-)[A-Za-z0-9]+)*\\.[A-Za-z0-9]+$";
        if ($.trim(email) == "") {
            $("#mail_error").css("color", "red");
            $("#mail_error").text("请输入邮箱");
            return;
        }
        else if (!RegExp(email).test(emailVal)) {
            $("#mail_error").css("color", "red");
            $("#mail_error").text("邮箱格式不正确");
            return;
        }
        else {
            $.getJSON(ajaxUrl, { oper: "checkUserEmail", userEmail: emailVal }, function(data) {
                if (data && data.msg == "exists") {
                    $("#mail_error").css("color", "red");
                    $("#mail_error").text("此邮箱已经存在！");
                    return;
                }
                else if (data && data.msg == "suc") {
                    if (!checkOtherName() || !checkTel()) {
                        return;

                    }
                    else if (!checkWay()) {
                        return;
                    }
                    else {
                        var code = $("#txtIdentityCode").val();
                        if ($.trim(code) == "") {
                            $("#codeError").css("color", "red");
                            $("#codeError").text("请输入验证码");
                        }
                        else {
                            $.getJSON(ajaxUrl, { oper: "checkCode", code: code }, function(data) {
                                if (data && data.msg == "err") {
                                    $("#codeError").css("color", "red");
                                    $("#codeError").text("验证码输入错误");
                                    return;
                                }
                                else {
                                    $("#registerForm").submit();
                                }
                            });
                        }
                    }
                }
            });
        }
    }
    else {
        return;
    }
}

//检验订单
//检验身份证
function checkNo(obj) {
    var number = $(obj).val();
    var oid = $(obj).attr("id");
    var soid = oid.split('_');
    var type = $("#type_" + soid[1] + "_" + soid[2]).val();
    if (type == "身份证") {
        if (number.length == 18) {
            var sex = number.substring(16, 17);
            var year = number.substring(6, 10);
            var month = number.substring(10, 12);
            var day = number.substring(12, 14);

            if (sex % 2 == 0) {
                $("#gender_" + soid[1] + "_" + soid[2] + " option[value='女']").attr('selected', 'selected');
            }
            else {
                $("#gender_" + soid[1] + "_" + soid[2] + " option[value='男']").attr('selected', 'selected');
            }
            var date = year + "-" + month + "-" + day;
            $("#birth_" + soid[1] + "_" + soid[2]).val(date);
        }
    }
}
//复制信息
function copyLine(number) {
    var isCheck = $("#aggree_" + number + "").prop("checked");
    if (isCheck) {
        $("#name_1_" + number + "").val($("#name_0_" + number + "").val());
        $("#type_1_" + number + " option[value='" + $("#type_0_" + number + "").val() + "']").attr('selected', 'selected');
        $("#gender_1_" + number + " option[value='" + $("#gender_0_" + number + "").val() + "']").attr("selected", 'selected');
        $("#no_1_" + number + "").val($("#no_0_" + number + "").val());
        $("#birth_1_" + number + "").val($("#birth_0_" + number + "").val());
    }
}
//添加行
function addAirLine() {
    var arr = $($("tr[id^='tr_']").last()).children("TD");
    var maxline = $(arr[0]).text().trim();

    var number = parseInt(maxline) + 1;

    var content = "<tr id=\"tr_" + number + "\"><td rowspan=\"3\" class=\"line\">" + number + "</td><td>投保人<br /><br />被投保人</td><td><input id=\"name_0_" + number + "\" name=\"no_0_" + number + "\" type=\"text\" size=\"8\" /><input id=\"name_1_" + number + "\" name=\"name_1_" + number + "\"type=\"text\" size=\"8\" /></td><td><select id=\"type_0_" + number + "\" name=\"type_0_" + number + "\"><option value=\"身份证\">身份证</option><option value=\"护照\">护照</option><option value=\"军官证\">军官证</option><option value=\"其他\">其他</option></select><select id=\"type_1_" + number + "\" name=\"type_1_" + number + "\"><option value=\"身份证\">身份证</option><option value=\"护照\">护照</option><option value=\"军官证\">军官证</option><option value=\"其他\">其他</option></select></td><td><input id=\"no_0_" + number + "\" name=\"no_0_" + number + "\" type=\"text\" size=\"20\" onblur=\"checkNo(this);\" onkeyup=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onmouseover=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))\" /><input id=\"no_1_" + number + "\" name=\"no_1_" + number + "\" type=\"text\" size=\"20\" onblur=\"checkNo(this);\" onkeyup=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onmouseover=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))\" /></td><td><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\"  data-date-format=\"yyyy-mm-dd\"><input id=\"birth_0_" + number + "\" name=\"birth_0_" + number + "\" type=\"text\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\" data-date-format=\"yyyy-mm-dd\"><input id=\"birth_1_" + number + "\" name=\"birth_1_" + number + "\" type=\"text\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div></td><td><select id=\"gender_0_" + number + "\" name=\"gender_0_" + number + "\"><option value=\"男\" selected=\"selected\">男</option><option value=\"女\">女</option></select><select id=\"gender_1_" + number + "\" name=\"gender_1_" + number + "\"><option value=\"男\" selected=\"selected\">男</option><option value=\"女\">女</option></select></td><td><input id=\"aggree_" + number + "\" name=\"aggree_" + number + "\" type=\"checkbox\" onclick=\"copyLine('" + number + "');\" />是被保人本人</td></tr><tr><td>被投保人电话</td><td>生效日期</td><td>航班号</td><td>电子邮箱</td><td colspan=\"3\">购买份数</td></tr><tr><td><input type=\"text\" id=\"tel_" + number + "\" size=\"15\" /></td><td><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\" data-date-format=\"yyyy-mm-dd\"><input type=\"text\" id=\"active_" + number + "\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div></td><td><input type=\"text\" id=\"flight_" + number + "\" size=\"15\" /></td><td><input type=\"text\" id=\"email_" + number + "\" /></td><td colspan=\"2\"><select id=\"num_" + number + "\" name=\"num_" + number + "\"><option value=\"1\">1份</option><option value=\"2\">2份</option></select></td><td><a onclick=\"delLine(this);\" style=\"cursor:pointer;\">删除</a></td></tr>";
    $("#personList").append(content);
    $('.input-append').datepicker();
}


//添加行
function addTravelLine() {
    var arr = $($("tr[id^='tr_']").last()).children("TD");
    var maxline = $(arr[0]).text().trim();

    var number = parseInt(maxline) + 1;

    var content = "<tr id=\"tr_" + number + "\"><td rowspan=\"3\" class=\"line\">" + number + "</td><td>投保人<br /><br />被投保人</td><td><input id=\"name_0_" + number + "\" name=\"no_0_" + number + "\" type=\"text\" size=\"8\" /><input id=\"name_1_" + number + "\" name=\"name_1_" + number + "\"type=\"text\" size=\"8\" /></td><td><select id=\"type_0_" + number + "\" name=\"type_0_" + number + "\"><option value=\"身份证\">身份证</option><option value=\"护照\">护照</option><option value=\"军官证\">军官证</option><option value=\"其他\">其他</option></select><select id=\"type_1_" + number + "\" name=\"type_1_" + number + "\"><option value=\"身份证\">身份证</option><option value=\"护照\">护照</option><option value=\"军官证\">军官证</option><option value=\"其他\">其他</option></select></td><td><input id=\"no_0_" + number + "\" name=\"no_0_" + number + "\" type=\"text\" size=\"20\" onblur=\"checkNo(this);\" onkeyup=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onmouseover=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))\" /><input id=\"no_1_" + number + "\" name=\"no_1_" + number + "\" type=\"text\" size=\"20\" onblur=\"checkNo(this);\" onkeyup=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onmouseover=\"value=value.replace(/[^a-zA-Z0-9]/g,'')\" onbeforepaste=\"clipboardData.setData('text',clipboardData.getData('text').replace(/[^a-zA-Z0-9]/g,''))\" /></td><td><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\"  data-date-format=\"yyyy-mm-dd\"><input id=\"birth_0_" + number + "\" name=\"birth_0_" + number + "\" type=\"text\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\" data-date-format=\"yyyy-mm-dd\"><input id=\"birth_1_" + number + "\" name=\"birth_1_" + number + "\" type=\"text\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div></td><td><select id=\"gender_0_" + number + "\" name=\"gender_0_" + number + "\"><option value=\"男\" selected=\"selected\">男</option><option value=\"女\">女</option></select><select id=\"gender_1_" + number + "\" name=\"gender_1_" + number + "\"><option value=\"男\" selected=\"selected\">男</option><option value=\"女\">女</option></select></td><td><input id=\"aggree_" + number + "\" name=\"aggree_" + number + "\" type=\"checkbox\" onclick=\"copyLine('" + number + "');\" />是被保人本人</td></tr><tr><td>被投保人电话</td><td>生效日期</td><td>旅行线路</td><td>电子邮箱</td><td colspan=\"3\">购买份数</td></tr><tr><td><input type=\"text\" id=\"tel_" + number + "\" size=\"15\" /></td><td><div class=\"input-append date\" data-date=\"@DateTime.Now.AddMonths(-1).ToString('yyyy-MM-dd')\" data-date-format=\"yyyy-mm-dd\"><input type=\"text\" id=\"active_" + number + "\" size=\"8\" data-date-format=\"yyyy-mm-dd\" /><span class=\"add-on\"><i class=\"icon-calendar\"></i></span></div></td><td><input type=\"text\" id=\"travel_" + number + "\" size=\"15\" /></td><td><input type=\"text\" id=\"email_" + number + "\" /></td><td colspan=\"2\"><select id=\"num_" + number + "\" name=\"num_" + number + "\"><option value=\"1\">1份</option><option value=\"2\">2份</option></select></td><td><a onclick=\"delLine(this);\" style=\"cursor:pointer;\">删除</a></td></tr>";
    $("#personList").append(content);
    $('.input-append').datepicker();
}

//删除行
function delLine(obj) {
    $(obj).parent().parent().prev().prev().remove();
    $(obj).parent().parent().prev().remove();
    $(obj).parent().parent().remove();
}
function prepairAirOrder(pid) {
    var productId = pid;
    var arrList = new Array();
    $("tr[id^='tr_']").each(function() {
        var arr = $(this).children("TD");
        var number = $(arr[0]).text().trim();
        arrList.push($("#name_0_" + number).val());
        arrList.push($("#type_0_" + number).val());
        arrList.push($("#no_0_" + number).val());
        arrList.push($("#birth_0_" + number).val());
        arrList.push($("#gender_0_" + number).val());

        arrList.push($("#name_1_" + number).val());
        arrList.push($("#type_1_" + number).val());
        arrList.push($("#no_1_" + number).val());
        arrList.push($("#birth_1_" + number).val());
        arrList.push($("#gender_1_" + number).val());

        arrList.push($("#tel_" + number).val());
        arrList.push($("#active_" + number).val());
        arrList.push($("#num_" + number).val());
        arrList.push($("#flight_" + number).val());
        arrList.push($("#email_" + number).val());
    });
    CreateOrder(arrList, pid);
}

function prepairTravelOrder(pid) {
    var productId = pid;
    var arrList = new Array();
    $("tr[id^='tr_']").each(function() {
        var arr = $(this).children("TD");
        var number = $(arr[0]).text().trim();
        arrList.push($("#name_0_" + number).val());
        arrList.push($("#type_0_" + number).val());
        arrList.push($("#no_0_" + number).val());
        arrList.push($("#birth_0_" + number).val());
        arrList.push($("#gender_0_" + number).val());

        arrList.push($("#name_1_" + number).val());
        arrList.push($("#type_1_" + number).val());
        arrList.push($("#no_1_" + number).val());
        arrList.push($("#birth_1_" + number).val());
        arrList.push($("#gender_1_" + number).val());

        arrList.push($("#tel_" + number).val());
        arrList.push($("#active_" + number).val());
        arrList.push($("#num_" + number).val());
        arrList.push($("#travel_" + number).val());
        arrList.push($("#email_" + number).val());
    });
    CreateOrder(arrList, pid);
}

//创建订单
function CreateOrder(arrList, productId) {
    $.ajax({
        type: 'POST',
        url: ajaxUrl,
        data: { oper: "CreateOrderInfo", arrList: arrList, productId: productId },
        success: function(request) {
            if (request != null) {
                if (request == "absence") {
                    alert("信息未填写完整，请填写所有信息");
                }
                else if (request == "null") {
                    alert("信息出现错误");
                }
                else if (request == "err") {
                    alert("提交订单出错");
                }
                else if (request == "age") {
                    alert("订单中包含有承保年龄不符");
                }
                else if (request == "count") {
                    alert("超过了最大限购额");
                }
                else {
                    location.href = "AddOrderSucceed.aspx?id=" + request;
                }
            }
        }
    });
}

function UploadAirFile() {
    var fileObj = document.getElementById("file").files[0]; // js 获取文件对象

    // FormData 对象
    var form = new FormData();
    form.append("oper", "UploadAir");                        // 可以增加表单数据
    form.append("file", fileObj);
    form.append("pt", "air");                                 // 文件对象

    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200 || xhr.status == 0) {
                // 取得返回字符串
                var resp = xhr.responseText;
                // 构造返回JSON对象的方法
                var func = new Function("return " + resp);
                // 得到JSON对象
                var json = func();
                if (json.msg == "suc") {
                    var arr = $($("tr[id^='tr_']").last()).children("TD");
                    var maxline = $(arr[0]).text().trim();
                    var number = maxline;
                    // 显示返回结果
                    $.each(json.data, function(index, item) {
                        if (Number(number) > 1) {
                            addAirLine();
                            number++;
                        }
                        else if (Number(index) > 0) {
                            addAirLine();
                            number = Number(number) + Number(index);
                        }
                        $("#name_0_" + number).val(item.name);
                        $("#type_0_" + number + " option[value='" + item.type + "']").attr('selected', 'selected');
                        $("#no_0_" + number).val(item.no);
                        $("#birth_0_" + number).val(item.birth);
                        $("#gender_0_" + number + " option[value='" + item.gender + "']").attr('selected', 'selected');
                        $("#name_1_" + number).val(item.name1);
                        $("#type_1_" + number + " option[value='" + item.type1 + "']").attr('selected', 'selected');
                        $("#no_1_" + number).val(item.no1);
                        $("#birth_1_" + number).val(item.birth1);
                        $("#gender_1_" + number + " option[value='" + item.gender1 + "']").attr('selected', 'selected');
                        $("#tel_" + number).val(item.tel);
                        $("#active_" + number).val(item.per);
                        $("#num_" + number + " option[value='1']").attr('selected', 'selected');
                        $("#flight_" + number).val(item.flight);
                        $("#email_" + number).val(item.email);
                    });
                    $("#progressBar").css("visibility", "hidden");
                    $("#percentage").css("visibility", "hidden");
                }
                else
                    alert("数据错误");
            }
        }
    };
    xhr.open("post", ajaxUrl, true);
    xhr.onload = function() {
        alert("上传完成");
    };
    $("#progressBar").css("visibility", "");
    $("#percentage").css("visibility", "");
    xhr.upload.addEventListener("progress", progressFunction, false);

    xhr.send(form);
}

function UploadTravelFile() {
    var fileObj = document.getElementById("file").files[0]; // js 获取文件对象

    // FormData 对象
    var form = new FormData();
    form.append("oper", "UploadTravel");                        // 可以增加表单数据
    form.append("file", fileObj);
    form.append("pt", "air");                                 // 文件对象

    // XMLHttpRequest 对象
    var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if (xhr.status == 200 || xhr.status == 0) {
                // 取得返回字符串
                var resp = xhr.responseText;
                // 构造返回JSON对象的方法
                var func = new Function("return " + resp);
                // 得到JSON对象
                var json = func();
                if (json.msg == "suc") {
                    var arr = $($("tr[id^='tr_']").last()).children("TD");
                    var maxline = $(arr[0]).text().trim();
                    var number = maxline;
                    // 显示返回结果
                    $.each(json.data, function(index, item) {
                        if (Number(number) > 1) {
                            addTravelLine();
                            number++;
                        }
                        else if (Number(index) > 0) {
                            addTravelLine();
                            number = Number(number) + Number(index);
                        }
                        $("#name_0_" + number).val(item.name);
                        $("#type_0_" + number + " option[value='" + item.type + "']").attr('selected', 'selected');
                        $("#no_0_" + number).val(item.no);
                        $("#birth_0_" + number).val(item.birth);
                        $("#gender_0_" + number + " option[value='" + item.gender + "']").attr('selected', 'selected');
                        $("#name_1_" + number).val(item.name1);
                        $("#type_1_" + number + " option[value='" + item.type1 + "']").attr('selected', 'selected');
                        $("#no_1_" + number).val(item.no1);
                        $("#birth_1_" + number).val(item.birth1);
                        $("#gender_1_" + number + " option[value='" + item.gender1 + "']").attr('selected', 'selected');
                        $("#tel_" + number).val(item.tel);
                        $("#active_" + number).val(item.per);
                        $("#num_" + number + " option[value='1']").attr('selected', 'selected');
                        $("#travel_" + number).val(item.travel);
                        $("#email_" + number).val(item.email);
                    });
                    $("#progressBar").css("visibility", "hidden");
                    $("#percentage").css("visibility", "hidden");
                }
                else
                    alert("数据错误");
            }
        }
    };
    xhr.open("post", ajaxUrl, true);
    xhr.onload = function() {
        alert("上传完成");
    };
    $("#progressBar").css("visibility", "");
    $("#percentage").css("visibility", "");
    xhr.upload.addEventListener("progress", progressFunction, false);

    xhr.send(form);
}

function progressFunction(evt) {
    var progressBar = document.getElementById("progressBar");
    var percentageDiv = document.getElementById("percentage");
    if (evt.lengthComputable) {
        progressBar.max = evt.total;
        progressBar.value = evt.loaded;
        percentageDiv.innerHTML = Math.round(evt.loaded / evt.total * 100) + "%";
    }
}
//取消订单
function CancelOrder(orderId) {
    if (confirm("是否取消该订单？")) {
        $.getJSON(ajaxUrl, { oper: "CancelOrder", orderId: orderId }, function(data) {
            if (data && data.msg == "suc") {
                location.href = "OrderDetail.aspx?id=" + orderId;
            }
            else {
                alert("取消失败");
            }
        });
    }
}
//退保
function cancelPolicy(orderId, policyId, time) {
    if (confirm("是否确认退保？")) {
        $.getJSON(ajaxUrl, { oper: "CancelPolicy", orderId: orderId, policyId: policyId, time: time }, function(data) {
            if (data && data.msg == "suc") {
                alert("退保成功");
                location.href = "PolicyList.aspx";
            }
            else if (data && data.msg == "time") {
                alert("保单起期已经过了");
                location.href = "PolicyList.aspx";
            }
            else {
                alert("退保失败");
            }
        });
    }
}

H = {};
H.app = {};
H.app.center = H.app.center || {};
H.app.center.order = {
    orderinit: function() {
        $("table .js_policy_problem").hover(function() {
            var a = $(this),
				b = a.parent();
            clearTimeout(a.data("timer"));
            b.addClass("status_layout_on");
            if (!a.data("requested")) {
                var c = '<table class="table_info" style="width:100%"><thead><tr><td>问题描述</td></tr></thead><tbody class="js_tbody"><tr><td style="text-align:center;padding:5px">正在请求，请稍侯...</td></tr></tbody><tfoot class="js_tfooter" style="display:none"><tr><td></td></tr></tfoot></table>';
                b.find(".order_box .layout_bd").html(c);
                a.data("requested", true);
                b.find(".layout_popup").unbind("mouseenter").unbind("mouseleave").hover(function() {
                    clearTimeout(a.data("timer"));
                    b.addClass("status_layout_on")
                }, function() {
                    b.removeClass("status_layout_on")
                });
                $.ajax({
                    type: "get",
                    url: ajaxUrl + "?oper=policyproblem&pid=" + a.attr("policy_id"),
                    timeout: 1000 * 10,
                    dataType: "json",
                    success: function(g) {
                        var l = "",
							m = "",
							e = null;
                        if (g && g.msg == "suc") {
                            var j = g.data;
                            $.each(j, function(index, item) {
                                l += "<tr><td>" + item.problem + "</td></tr>"
                            });
                        } else {
                            l = '<tr><td>' + (g.data || "查询问题描述失败") + "</td></tr>";
                        }
                        b.find(".order_box .js_tbody").html(l);
                    },
                    error: function() {
                        a.data("requested", false)
                    }
                })
            }
        }, function() {
            var a = $(this);
            clearTimeout(a.data("timer"));
            var b = setTimeout(function() {
                a.parent().removeClass("status_layout_on")
            }, 100);
            a.data("timer", b)
        });
        $("table .js_order_address").hover(function() {
            var a = $(this),
				b = a.parent();
            clearTimeout(a.data("timer"));
            b.addClass("status_layout_on");
            if (!a.data("requested")) {
                var c = '<table class="table_info" style="width:100%"><colgroup><col class="col1"><col class="col2"><col class="col3"><col class="col4"><col class="col5"></colgroup><thead><tr><td>省份</td><td>城市/区</td><td>地址</td><td>联系人</td><td>联系电话</td></tr></thead><tbody class="js_tbody"><tr><td style="text-align:center;padding:5px" colspan="5">正在请求，请稍侯...</td></tr></tbody><tfoot class="js_tfooter" style="display:none"><tr><td></td></tr></tfoot></table>';
                b.find(".order_box .layout_bd").html(c);
                a.data("requested", true);
                b.find(".layout_popup").unbind("mouseenter").unbind("mouseleave").hover(function() {
                    clearTimeout(a.data("timer"));
                    b.addClass("status_layout_on")
                }, function() {
                    b.removeClass("status_layout_on")
                });
                $.ajax({
                    type: "get",
                    url: ajaxUrl + "?oper=GetOrderAddress&oid=" + a.attr("order_id"),
                    timeout: 1000 * 10,
                    dataType: "json",
                    success: function(g) {
                        var l = "",
							m = "",
							e = null;
                        if (g && g.msg == "suc") {
                            var j = g.data;
                            $.each(j, function(index, item) {
                                l += "<tr><td>" + item.province + "</td><td>" + item.city + "</td><td>" + item.address + "</td><td>" + item.contact + "</td><td>" + item.tel + "</td></tr>"
                            });
                        } else {
                            l = '<tr><td colspan="5">' + (g.data || "查询配送地址失败") + "</td></tr>";
                        }
                        b.find(".order_box .js_tbody").html(l);
                    },
                    error: function() {
                        a.data("requested", false)
                    }
                })
            }
        }, function() {
            var a = $(this);
            clearTimeout(a.data("timer"));
            var b = setTimeout(function() {
                a.parent().removeClass("status_layout_on")
            }, 100);
            a.data("timer", b)
        });
        $("table .js_order_fedex").hover(function() {
            var a = $(this),
				b = a.parent();
            clearTimeout(a.data("timer"));
            b.addClass("status_layout_on");
            if (!a.data("requested")) {
                var c = '<table class="table_info" style="width:100%"><thead><tr><td>物流公司</td><td>物流单号</td></tr></thead><tbody class="js_tbody"><tr><td style="text-align:center;padding:5px" colspan="2">正在请求，请稍侯...</td></tr></tbody><tfoot class="js_tfooter" style="display:none"><tr><td></td></tr></tfoot></table>';
                b.find(".order_box .layout_bd").html(c);
                a.data("requested", true);
                b.find(".layout_popup").unbind("mouseenter").unbind("mouseleave").hover(function() {
                    clearTimeout(a.data("timer"));
                    b.addClass("status_layout_on")
                }, function() {
                    b.removeClass("status_layout_on")
                });
                $.ajax({
                    type: "get",
                    url: ajaxUrl + "?oper=GetOrderFedex&oid=" + a.attr("order_id"),
                    timeout: 1000 * 10,
                    dataType: "json",
                    success: function(g) {
                        var l = "",
							m = "",
							e = null;
                        if (g && g.msg == "suc") {
                            var j = g.data;
                            $.each(j, function(index, item) {
                                l += "<tr><td>" + item.company + "</td><td>" + item.no + "</td></tr>"
                            });
                        } else {
                            l = '<tr><td colspan="2">' + (g.data || "查询物流信息失败") + "</td></tr>";
                        }
                        b.find(".order_box .js_tbody").html(l);
                    },
                    error: function() {
                        a.data("requested", false)
                    }
                })
            }
        }, function() {
            var a = $(this);
            clearTimeout(a.data("timer"));
            var b = setTimeout(function() {
                a.parent().removeClass("status_layout_on")
            }, 100);
            a.data("timer", b)
        });
    }
};
function GetCity() {
    var value = $("#province").val();
    var id;
    var money;
    var content;
    if ($.trim(value) != "") {
        var arr = new Array();
        arr = value.split("|");
        if (arr.length > 1) {
            id = arr[0];
            money = arr[1];
            $.ajax({
                type: "get",
                url: ajaxUrl + "?oper=GetCityListByProvinceId&provinceId=" + id,
                timeout: 1000 * 10,
                dataType: "json",
                success: function(g) {
                    if (g && g.msg == "suc") {
                        var j = g.data;
                        $.each(j, function(index, item) {
                            content += "<option value=\"" + item.name + "\">" + item.name + "</option>";
                        });
                    }
                    else {
                        $("#city").html("");
                    }
                    $("#city").html(content);
                },
                error: function() {
                    a.data("requested", false)
                }
            });
        }
    }
    var shipping = $("#shipping").val();
    if (shipping == "1") {
        $("#money").html(money);
    }
    else {
        $("#money").html("0");
    }
}
function addInvoiceOrder() {
    var policy = $("#policy").val();
    var count = $("#count").val();
    var shipping = $("#shipping").val();
    var province = $("#province").val();
    var city = $("#city").val();
    var address = $("#address").val();
    var contacts = $("#contacts").val();
    var contactsTel = $("#contactsTel").val();
    var money = $("#money").html();
    if (shipping == "3" || shipping == "2") {
        money = "0";
    }
    if ($.trim(count) == "") {
        alert("请输入申领张数");
        return false;
    }
    else if ($.trim(address) == "" && (shipping == "1" || shipping == "2")) {
        alert("请输入地址");
        return false;
    }
    else if ($.trim(contacts) == "" && (shipping == "1" || shipping == "2")) {
        alert("请输入联系人");
        return false;
    }
    else if ($.trim(contactsTel) == "" && (shipping == "1" || shipping == "2")) {
        alert("请输入联系电话");
        return false;
    }
    else {
        $.getJSON(ajaxUrl, { oper: "AddInvoiceOrder", policy: policy, count: count, shipping: shipping, province: province, city: city, address: address, contacts: contacts, contactsTel: contactsTel, money: money }, function(data) {
            if (data && data.msg == "suc") {
                location.href = "InvoiceOrderSucceed.aspx?id=" + data.orderId;
            }
        });
    }
}
